package servlet;

import dao.BooksDao;
import dao.impl.BooksDaoImpl;
import pojo.Books;
import service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddBookServlet extends HttpServlet{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String bCoad = request.getParameter("bCoad");
        String bname = request.getParameter("bName");
        int tID = Integer.parseInt(request.getParameter("tID"));
        String bAutho = request.getParameter("bAutho");
        int bNum = Integer.parseInt(request.getParameter("bNum"));


        // 用good封装所有的的变量，减少参数传递的个数
        Books books = new Books();
        books.setbCode(bCoad);
        books.setbName(bname);
        books.settID(tID);
        books.setbAutho(bAutho);
        books.setbNum(bNum);

        BooksDao booksDao = new BooksDaoImpl();
        BookServiceImpl bookService = new BookServiceImpl();
        bookService.setBooksDao(booksDao);

        int res = bookService.addBook(books);

        if (res > 0){
            request.setAttribute("msg","书籍添加成功");
        }else {
            request.setAttribute("msg","书籍添加失败");
        }
        request.getRequestDispatcher("BookServlet").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
